package com.treasure.mall.zbqmallapi.controller;

import com.treasure.mall.biz.dto.pay.PayDTO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.remote.quickpay.WechatPayService;
import com.treasure.mall.biz.remote.quickpay.dto.WxTradeOrderDTO;
import com.treasure.mall.biz.service.core.pay.PayService;
import com.treasure.mall.zbq.common.annotation.CurrentUser;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.ienum.order.OrderEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/7/17
 */
@RestController
@RequestMapping("/order/biz")
public class BizOrderController {
    @Autowired
    private PayService payService;
    @Autowired
    private WechatPayService wechatPayService;

    /**
     * 增值业务支付
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/pay")
    public ApiResult pay(
            @CurrentUser UserPO user,
            PayDTO payDTO,
            HttpServletRequest request
    ) {
        payDTO.setUserPO(user);
        WxTradeOrderDTO tradeOrderDTO = payService.pay(payDTO);
        if (payDTO.getSource() == 1) {
            tradeOrderDTO.setTradeType(OrderEnums.TradeType.APP);
        }
        return ApiResult.ok(wechatPayService.JsApiBizPackage(tradeOrderDTO, request));
    }
}
